{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tick 1601002364.428206\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "print(\"tick\", time.time()) // in seconds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "local time.struct_time(tm_year=2020, tm_mon=9, tm_mday=25, tm_hour=10, tm_min=55, tm_sec=27, tm_wday=4, tm_yday=269, tm_isdst=0)\n"
     ]
    }
   ],
   "source": [
    "localtime = time.localtime(time.time())\n",
    "print(\"local\", localtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['_STRUCT_TM_ITEMS',\n",
       " '__doc__',\n",
       " '__loader__',\n",
       " '__name__',\n",
       " '__package__',\n",
       " '__spec__',\n",
       " 'altzone',\n",
       " 'asctime',\n",
       " 'clock',\n",
       " 'ctime',\n",
       " 'daylight',\n",
       " 'get_clock_info',\n",
       " 'gmtime',\n",
       " 'localtime',\n",
       " 'mktime',\n",
       " 'monotonic',\n",
       " 'monotonic_ns',\n",
       " 'perf_counter',\n",
       " 'perf_counter_ns',\n",
       " 'process_time',\n",
       " 'process_time_ns',\n",
       " 'sleep',\n",
       " 'strftime',\n",
       " 'strptime',\n",
       " 'struct_time',\n",
       " 'time',\n",
       " 'time_ns',\n",
       " 'timezone',\n",
       " 'tzname',\n",
       " 'tzset']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# time to string\n",
    "strtime = time.asctime(time.localtime(time.time()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fri Sep 25 10:56:20 2020\n"
     ]
    }
   ],
   "source": [
    "print(strtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fri Sep 25 10:52:44 2020\n",
      "2020 09 25 10:52:44\n",
      "time.struct_time(tm_year=2020, tm_mon=9, tm_mday=25, tm_hour=10, tm_min=52, tm_sec=44, tm_wday=4, tm_yday=269, tm_isdst=-1)\n"
     ]
    }
   ],
   "source": [
    "# all formats: https://www.tutorialspoint.com/python/time_strftime.htm\n",
    "# long to time\n",
    "# Fri Sep 25 10:52:44 2020  本地时区\n",
    "t = time.localtime(1601002364)\n",
    "print(time.asctime(t))\n",
    "# long to string\n",
    "print(time.strftime(\"%Y %m %d %H:%M:%S\", t))\n",
    "\n",
    "\n",
    "# string to long\n",
    "print(time.strptime(\"2020 09 25 10:52:44\", \"%Y %m %d %H:%M:%S\"))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "fsum = lambda a, b : a+b\n",
    "print(fsum(1, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['/Users/edward/projects/helloworlds/pythonTutorial', '/opt/anaconda3/lib/python37.zip', '/opt/anaconda3/lib/python3.7', '/opt/anaconda3/lib/python3.7/lib-dynload', '', '/opt/anaconda3/lib/python3.7/site-packages', '/opt/anaconda3/lib/python3.7/site-packages/aeosa', '/opt/anaconda3/lib/python3.7/site-packages/IPython/extensions', '/Users/edward/.ipython']\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "print(sys.path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.18.1\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "print(numpy.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "testfile = open(\"test\", \"w+\")\n",
    "testfile.write(\"okay\\n\")\n",
    "testfile.write(\"anotherline\")\n",
    "testfile.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "okay\n",
      "anotherline\n"
     ]
    }
   ],
   "source": [
    "r = open(\"test\", \"r\")\n",
    "x = r.read(50) # 50bytes\n",
    "print(x)\n",
    "r.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "okay\n",
      "\n",
      "anotherline\n"
     ]
    }
   ],
   "source": [
    "f = open(\"test\",\"r\")  \n",
    "lines = f.readlines()#读取全部内容  \n",
    "for line in lines:  \n",
    "    print(line) \n",
    "f.close()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['python.md',\n",
       " 'image-20200921171623832.png',\n",
       " 'test',\n",
       " 'image-20200925110210978.png',\n",
       " 'image-20200921172830427.png',\n",
       " 'image-20200923194048067.png',\n",
       " 'image-20200921171640454.png',\n",
       " '.ipynb_checkpoints',\n",
       " 'image-20200923155359144.png',\n",
       " 'tutorial.ipynb',\n",
       " 'image-20200921171303512.png']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.listdir(os.getcwd())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "start cal\n",
      "Error divide division by zero\n",
      "final part\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"<ipython-input-37-0de925d1098f>\", line 4, in <module>\n",
      "    x = 1/0\n",
      "ZeroDivisionError: division by zero\n"
     ]
    }
   ],
   "source": [
    "import traceback\n",
    "try:\n",
    "    print(\"start cal\")\n",
    "    x = 1/0\n",
    "    print(\"finish cal ok\")\n",
    "# except (RuntimeError, TypeError, NameError):    \n",
    "except Exception as e:\n",
    "    print(\"Error divide {0}\".format(e) )\n",
    "    traceback.print_exc()\n",
    "    #traceback.print_exception(e)\n",
    "finally:\n",
    "    print(\"final part\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function print_exc in module traceback:\n",
      "\n",
      "print_exc(limit=None, file=None, chain=True)\n",
      "    Shorthand for 'print_exception(*sys.exc_info(), limit, file)'.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(traceback.print_exc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "123",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-39-10aba17d32f4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      2\u001b[0m     \u001b[0;32mraise\u001b[0m \u001b[0mNameError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m123\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-39-10aba17d32f4>\u001b[0m in \u001b[0;36mf\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m     \u001b[0;32mraise\u001b[0m \u001b[0mNameError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m123\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: 123"
     ]
    }
   ],
   "source": [
    "def f(x):\n",
    "    raise NameError(x)\n",
    "\n",
    "f(123)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 6, 10, 14]\n"
     ]
    }
   ],
   "source": [
    "xx = [1, 3, 5, 7]\n",
    "yy = [x*2 for x in xx]\n",
    "print(yy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Fruit:\n",
    "    count = 0\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "    def getDisplay():\n",
    "        return name + \"me\"\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "matchObj.group() :  Cats are smarter than dogs\n",
      "matchObj.group(1) :  Cats\n",
      "matchObj.group(2) :  smarter\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "#!/usr/bin/python\n",
    "import re\n",
    "\n",
    "line = \"Cats are smarter than dogs\"\n",
    "\n",
    "matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)\n",
    "\n",
    "if matchObj:\n",
    "   print(\"matchObj.group() : \", matchObj.group())\n",
    "   print(\"matchObj.group(1) : \", matchObj.group(1))\n",
    "   print(\"matchObj.group(2) : \", matchObj.group(2))\n",
    "else:\n",
    "   print(\"No match!!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'django'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-7-5fa2257f2ea7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mdjango\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'django'"
     ]
    }
   ],
   "source": [
    "#import django"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
